	
foreach Y of numlist 2012(1)2018 {
	global Y = `Y'
	
	capture drop EG
	gen EG = gaver_tot-don_cap if year == ${Y}
	global appendfilename = "_year${Y}"
	*-----------------------------------------------------------------*
	* Basic variables for bunch_count
	*-----------------------------------------------------------------*

	
	capture drop *G*bin*
	gen long _EG_bin = floor(EG/500)*500
	bysort _EG_bin: gen _EG_bin_N = _N
	by _EG_bin: gen _EG_bin_n = _n



	* Needed to calculate relative excess mass / elasticity
	sum don_cap if inrange(_EG_bin,-3500,3900) & !missing(_EG_bin_N) 
	global mean_don_cap = round(`r(mean)')
	sum don_rate if inrange(_EG_bin,-3500,3900) & !missing(_EG_bin_N) 
	global mean_don_rate = `r(mean)'
	

	sum _EG_bin_N if _EG_bin_n==1 & inrange(_EG_bin,-3500,3500)
	global Nmax = `r(max)'		
	global Nmax_divby_2 = ${Nmax}/2

	
	* do the bunching analysis using bunch_count	
	preserve
		keep if _EG_bin_n==1 & !missing(_EG_bin)
		capture bunch_count _EG_bin _EG_bin_N if _EG_bin_n==1, ig_low(-7) ig_high(7) low_bunch(-1) high_bunch(1) binwidth(500) plot(1) nboot(200) degree(5)
	restore
	
	graph save ${figures}/wdon1_bunch_count_pooled_adj${appendfilename}_RAW.gph, replace
	graph use ${figures}/wdon1_bunch_count_pooled_adj${appendfilename}_RAW.gph
		
		
		global b_est =subinstr(subinstr("abc"+string(round(b,0.01)),"abc.","abc0.",.),"abc","",.)
		global B_est = string(round(b*100/${mean_don_cap},0.01)) /* 100 is binwidth */
		global b_se = subinstr(subinstr("abc"+string(round(b_se,0.01)),"abc.","abc0.",.),"abc","",.)
		
		global Kbar = string(round(${mean_don_cap}))
		global tau_g =subinstr(subinstr("abc"+string(round(${mean_don_rate},0.0001)),"abc.","abc0.",.),"abc","",.)
		
		global e_est_ = (b*500/${mean_don_cap})/(-log(1-${mean_don_rate}))
		global e_est  ="-" + substr(subinstr(subinstr("abc"+string(round(${e_est_},0.01)),"abc.","abc0.",.),"abc","",.) + "0",1,4) /* keeps leading and lagging zeros..*/
		global e_se_ = (b_se*500/${mean_don_cap})/(-log(1-${mean_don_rate}))
		global e_se  =substr(subinstr(subinstr("abc"+string(round(${e_se_},0.01)),"abc.","abc0.",.),"abc","",.) + "0",1,4)
		
		* Fix 
		gr_edit .style.editstyle margin(medlarge) editcopy
		gr_edit .yaxis1.style.editstyle majorstyle( gridstyle( linestyle(color(gs14%0)) draw_min(yes) draw_max(yes))) editcopy
		gr_edit .plotregion1.plot1.style.editstyle marker(size(0.5)  symbol(circle) linestyle(color(blue)) fillcolor(blue)	) editcopy
		gr_edit .plotregion1.plot1.style.editstyle line(color(blue))	editcopy
		gr_edit .plotregion1.plot2.style.editstyle line(color(green) width(medthick)) editcopy
		gr_edit .plotregion1._xylines[1].style.editstyle linestyle(color(gs4%15) width(vvthick)) editcopy
		gr_edit .title.text = {"{stSerif: Bunching in ${Y}}"} 
		gr_edit .subtitle.text = {    "{stSerif:Estimated e=${e_est} (se=${e_se})}"  }
		*gr_edit .subtitle.text.Arrpush  "{stSerif:b=${b_est} (${b_se}), mean K{sub:t}=${Kbar}, {&tau}{sub:g,t}=${tau_g}}"  
		gr_edit .title.style.editstyle size(medlarge) color(black) margin(vsmall) editcopy
		gr_edit .subtitle.style.editstyle size(medium) color(blue) margin(small) editcopy
		
		gr_edit ..plotregion1.AddTextBox added_text editor  ${Nmax_divby_2} 3
		gr_edit ..plotregion1.added_text[1].style.editstyle linegap(0.5) color(gs5) editcopy
		gr_edit ..plotregion1.added_text[1].text = {}
		gr_edit ..plotregion1.added_text[1].text.Arrpush `"b=${b_est} (se=${b_se})"'
		gr_edit ..plotregion1.added_text[1].text.Arrpush `"K{sub:${Y}}=${Kbar}"'
		gr_edit ..plotregion1.added_text[1].text.Arrpush `"{&tau}{sup:g}{sub:${Y}}=${tau_g}"'
		
		gr_edit .xaxis1.reset_rule 15, tickset(major) ruletype(suggest) 
		foreach b of numlist 7(-1)-7 { /* has to be done backwards for some reason.. */
				local oldnum = `b'
				local newnum = round(${Kbar}+`b'*500)
				local newnum_string = " "
				if inlist(`b',-4,0,4) {
					local newnum_string = "`newnum'"
				}
				disp "`b' , `oldnum' , `newnum'"
				gr_edit .xaxis1.major.num_rule_ticks = 0
				gr_edit .xaxis1.edit_tick `b' `oldnum' "`newnum_string'", tickset(major)
			}
		
		
		gr_edit .yaxis1.title.text = {"Frequency"}

		gr_edit .xaxis1.title.text = {"Charitable Giving (NOK)"}

		gr_edit .style.editstyle boxstyle(linestyle(color(white))) editcopy
		
		graph save ${figures}/wdon1_bunch_count_pooled_adj${appendfilename}.gph, replace
}
		
		*[FIGURE A.19]
		
		#delimit;
		gr combine 	${figures}/wdon1_bunch_count_pooled_adj_year2012.gph
				${figures}/wdon1_bunch_count_pooled_adj_year2013.gph
				${figures}/wdon1_bunch_count_pooled_adj_year2014.gph
				${figures}/wdon1_bunch_count_pooled_adj_year2015.gph
				${figures}/wdon1_bunch_count_pooled_adj_year2016.gph
				${figures}/wdon1_bunch_count_pooled_adj_year2017.gph
				${figures}/wdon1_bunch_count_pooled_adj_year2018.gph,
		
		rows(4) cols(2) graphregion(color(white) margin(tiny tiny tiny tiny)) xsize(5.5) ysize(8)   imargin(1 6 1 1) iscale(0.5);
		#delimit cr
		
		gr save ${figures}/wdon1_bunch_year_by_year.gph, replace
		gr export ${figures}/wdon1_bunch_year_by_year.pdf,  as(pdf) replace
		
